# encoding: utf-8
class CreditosController < TransacoesController
#	rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found
	
	before_filter lambda { load_conta(Credito) }, :only => [ :new, :create, :update ]
	before_filter :load_credito, :only => [ :edit, :delete, :destroy, :update ]

	#
	#
	def new
		@credito = Credito.new
	end

	#
	#
	def create
		@credito = Credito.new(params[:credito])
		
		if @credito.save
			redirect_to conta_path(@conta), :notice => "Crédito efetuado com sucesso"
		else
			render :new
		end
	end

	#
	#
	def update
		if @credito.update_attributes(params[:credito])
			redirect_to conta_path(@conta), :notice => "Crédito alterado com sucesso"
		else
			render :new
		end
	end
	
	private
	
	#
	#	
	def load_credito
		begin
			@credito = Credito.find(params[:id])
			
			unless belongs_to_current_usuario?(@credito.conta)
				flash[:alert] = "Operação inválida"
				redirect_to contas_path and return
			end
		rescue ActiveRecord::RecordNotFound
			flash[:alert] = "Operação inválida"
			redirect_to contas_path and return		
		end
	end
	
end

